# iOS 手動安裝第三方庫 Objective-C to Swift

記錄一次使用 TimOliver/TOCropViewController (opens new window), 手動安裝Swift Projet


一開始新建一個Project

image-20200221135929638

下載 TimOliver/TOCropViewController (opens new window)

git clone https://github.com/TimOliver/TOCropViewController.git

目錄大概是這樣

.
├── Images
│   └── Donators
├── Objective-C
│   ├── TOCropViewController
│   │   ├── Categories
│   │   ├── Constants
│   │   ├── Models
│   │   ├── Resources
│   │   │   ├── ...
│   │   ├── Supporting
│   │   └── Views
│   ├── TOCropViewControllerExample
│   │   ├── ...
│   ├── TOCropViewControllerExample-Extension
│   └── TOCropViewControllerTests
├── Swift
│   ├── CropViewController
│   └── CropViewControllerExample
│       ├── Assets.xcassets
│       │   └── AppIcon.appiconset
│       └── Base.lproj
├── TOCropViewControllerExample.xcodeproj
│   ├── project.xcworkspace
│   │   └── xcshareddata
│   └── xcshareddata
│       └── xcschemes
├── buildkite
└── fastlane

根據作者的README (opens new window), swift project需要同時使用Objective-C/TOCropViewControllerSwift/CropViewController

All of the necessary source and resource files for TOCropViewController are in Objective-C/TOCropViewController, and all of the necessary Swift files are in Swift/CropViewController.

For Objective-C projects, copy just the TOCropViewController directory to your Xcode project. For Swift projects, copy both TOCropViewController and CropViewController to your project.

在剛剛新建的Project 點擊 Add Files to "xxx"...

image-20200221141329728

先選擇TOCropViewController/Objective-C/TOCropViewController, 記得點擊Create groups

image-20200221141456692

可以看到剛點加入的檔䅁會黃色,如果選了Create folder references會顯示藍色

image-20200221141814561

再將TOCropViewController/Swift/CropViewController的檔案加入TOCropViewController

image-20200221142954796

注意⚠️: 要把info.plist刪除,否則會有錯誤

image-20200221143050987

接下來是關鍵的一步建立{project name}-Bridging-Header.h

image-20200221143413492

//{project name}-Bridging-Header.h
#import "TOCropViewController.h" // add

要在設定加入image-20200221144046510

image-20200221144104735


# 測試

加入一張相片用作測試

image-20200221144449741

加一個Button

image-20200221144851179

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func click(_ sender: Any) {
        let image: UIImage = UIImage(named: "test-image")!
        let cropViewController = CropViewController(image: image)
        cropViewController.delegate = self
        present(cropViewController, animated: true, completion: nil)
    }
}

extension ViewController: CropViewControllerDelegate {
    func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        print(image)
    }
}
2020-02-21 14.52.01

image-20200221145322774

成功取得正確的結果image-20200221145348859

Last Updated: Fri Feb 21 2020 07:27:34 GMT+0000
贊助商連結
(adsbygoogle = window.adsbygoogle || []).push({});